package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.vo.CategoryVo;
import io.renren.modules.sys.vo.DormAreaInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.DormAreaEntity;
import io.renren.modules.sys.service.DormAreaService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author Lizhiwei
 * @email 123@qq.com
 * @date 2022-10-29 00:49:39
 */
@RestController
@RequestMapping("sys/dormarea")
public class DormAreaController extends AbstractController{
    @Autowired
    private DormAreaService dormAreaService;

//    @RequestMapping("/getDormInfoByDormId")
//    public R getDormInfoByDormId(@RequestParam(name = "dormId",required = false) Integer dormId){

    @RequestMapping("/getDeafultMenu")
    public R getDeafultMenu(@RequestParam(name = "dormId",required = false) Integer dormId){
        Integer[] integers = new Integer[3];
        if (dormId==null) {
            SysUserEntity user = getUser();
            dormId = user.getDormId();
        }
        integers[2]=dormId;
        DormAreaEntity entity = dormAreaService.getById(dormId);
        DormAreaEntity entity1 = dormAreaService.getById(entity.getParentId());
        integers[1]=entity1.getId();
        DormAreaEntity entity2 = dormAreaService.getById(entity1.getParentId());
        integers[0]=entity2.getId();

        return R.ok().put("data", integers);
    }


    @RequestMapping("/getInfo/{id}")
//    @RequiresPermissions("sys:dormarea:list")
    public R getInfo(@PathVariable("id") Integer id){
        DormAreaEntity entity = dormAreaService.getById(id);
        DormAreaInfoVo infoVo = new DormAreaInfoVo();
        BeanUtils.copyProperties(entity, infoVo);
        if (entity.getLevel()!=0){
            DormAreaEntity entity1 = dormAreaService.getById(entity.getParentId());
            infoVo.setFirstLevel(entity1.getName());
            if (entity.getLevel()==2){
                DormAreaEntity entity2 = dormAreaService.getById(entity1.getParentId());
                infoVo.setFirstLevel(entity2.getName());
                infoVo.setSecondLevel(entity1.getName());
            }else {
                infoVo.setFirstLevel(entity1.getName());
            }
        }
        return R.ok().put("data",infoVo);
    }
    /**
     * 获取分类数据
     * @return
     */
    @RequestMapping("/getCategory")
//    @RequiresPermissions("sys:dormarea:list")
    public R getCatelogy(){
        List<DormAreaEntity> category = dormAreaService.getCatelogy();
        return R.ok().put("data",category);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:dormarea:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = dormAreaService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:dormarea:info")
    public R info(@PathVariable("id") Integer id){
		DormAreaEntity dormArea = dormAreaService.getById(id);

        return R.ok().put("data", dormArea);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:dormarea:save")
    public R save(@RequestBody DormAreaEntity dormArea){
		dormAreaService.save(dormArea);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:dormarea:update")
    public R update(@RequestBody DormAreaEntity dormArea){
		dormAreaService.updateById(dormArea);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:dormarea:delete")
    public R delete(@RequestBody Integer[] ids){
		dormAreaService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
